package com.module.util;

import com.alibaba.fastjson.JSONObject;
import com.module.common.utils.HexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 多费率电表协议解析
 */
@Slf4j
public class BillionResult {

    /**
     * 电表主动上报协议解析
     * @param content
     * @return
     */
    public static JSONObject parseContent(String content) {
        String[] msgs = HexUtils.strChange(content, 2);
        try{
            //表号
            String meterNo = HexUtils.strChangeStrByLengthDesc(msgs, 1, 6, false);
            //数据长度
            int datalength = HexUtils.get10HexNum(msgs[9]);
            String[] datas = HexUtils.strChangeByLength(msgs, 10, datalength);
            int start = 4;
            // 小数位数
            String xsws = HexUtils.getHexNumByFixed(datas[start], "33");
            // 剩余金额
            start += 1;
            String syje = HexUtils.strChangeStrByLength(datas, start, 4, true);
            syje = HexUtils.getHexNumByFixed(syje.split(" "), "33", false);
            // 总有功电能
            start += 4;
            String value = HexUtils.strChangeStrByLength(datas, start, 4, true);
            String result = HexUtils.getHexNumByFixed(value.split(" "), "33", false);
            // 尖有功电能
            start += 4;
            String value1 = HexUtils.strChangeStrByLength(datas, start, 4, true);
            String result1 = HexUtils.getHexNumByFixed(value1.split(" "), "33", false);
            // 峰有功电能
            start += 4;
            String value2 = HexUtils.strChangeStrByLength(datas, start, 4, true);
            String result2 = HexUtils.getHexNumByFixed(value2.split(" "), "33", false);
            // 平有功电能
            start += 4;
            String value3 = HexUtils.strChangeStrByLength(datas, start, 4, true);
            String result3 = HexUtils.getHexNumByFixed(value3.split(" "), "33", false);
            // 谷有功电能
            start += 4;
            String value4 = HexUtils.strChangeStrByLength(datas, start, 4, true);
            String result4 = HexUtils.getHexNumByFixed(value4.split(" "), "33", false);
            // 正总有功电能
            start += 4;
            String zzygdn = HexUtils.strChangeStrByLength(datas, start, 4, true);
            zzygdn = HexUtils.getHexNumByFixed(zzygdn.split(" "), "33", false);
            // 正尖有功电能
            start += 4;
            String zjygdn = HexUtils.strChangeStrByLength(datas, start, 4, true);
            zjygdn = HexUtils.getHexNumByFixed(zjygdn.split(" "), "33", false);
            // 正峰有功电能
            start += 4;
            String zfygdn = HexUtils.strChangeStrByLength(datas, start, 4, true);
            zfygdn = HexUtils.getHexNumByFixed(zfygdn.split(" "), "33", false);
            // 正平有功电能
            start += 4;
            String zpygdn = HexUtils.strChangeStrByLength(datas, start, 4, true);
            zpygdn = HexUtils.getHexNumByFixed(zpygdn.split(" "), "33", false);
            // 正谷有功电能
            start += 4;
            String zgygdn = HexUtils.strChangeStrByLength(datas, start, 4, true);
            zgygdn = HexUtils.getHexNumByFixed(zgygdn.split(" "), "33", false);
            // 反总有功电能
            start += 4;
            String fzygdn = HexUtils.strChangeStrByLength(datas, start, 4, true);
            fzygdn = HexUtils.getHexNumByFixed(fzygdn.split(" "), "33", false);
            // 反尖有功电能
            start += 4;
            String fjygdn = HexUtils.strChangeStrByLength(datas, start, 4, true);
            fjygdn = HexUtils.getHexNumByFixed(fjygdn.split(" "), "33", false);
            // 反峰有功电能
            start += 4;
            String ffygdn = HexUtils.strChangeStrByLength(datas, start, 4, true);
            ffygdn = HexUtils.getHexNumByFixed(ffygdn.split(" "), "33", false);
            // 反平有功电能
            start += 4;
            String fpygdn = HexUtils.strChangeStrByLength(datas, start, 4, true);
            fpygdn = HexUtils.getHexNumByFixed(fpygdn.split(" "), "33", false);
            // 反谷有功电能
            start += 4;
            String fgygdn = HexUtils.strChangeStrByLength(datas, start, 4, true);
            fgygdn = HexUtils.getHexNumByFixed(fgygdn.split(" "), "33", false);
            // 电压
            start += 4;
            String voltage = HexUtils.strChangeStrByLength(datas, start, 4, true);
            voltage = HexUtils.getHexNumByFixed(voltage.split(" "), "33", false);
            // 电流
            start += 4;
            String galvanic = HexUtils.strChangeStrByLength(datas, start, 4, true);
            galvanic = HexUtils.getHexNumByFixed(galvanic.split(" "), "33", false);
            // 有功功率
            start += 4;
            String power = HexUtils.strChangeStrByLength(datas, start, 4, true);
            power = HexUtils.getHexNumByFixed(power.split(" "), "33", false);
            // 总有功需量功率
            start += 4;
            String zygxl = HexUtils.strChangeStrByLength(datas, start, 4, true);
            zygxl = HexUtils.getHexNumByFixed(zygxl.split(" "), "33", false);
            // 功率因数
            start += 4;
            String glys = HexUtils.strChangeStrByLength(datas, start, 2, true);
            glys = HexUtils.getHexNumByFixed(glys.split(" "), "33", false);
            // 信号
            start += 2;
            String csq = HexUtils.getHexNumByFixed(datas[start], "33");
            // 继电器状态 4D拉闸4E合闸
            start += 1;
            String jdqzt = datas[start];
            // imei
            start += 1;
            String imei = HexUtils.strChangeStrByLengthDesc(datas, start, 8, true);
            imei = HexUtils.getHexNumByFixed(imei.split(" "), "33", false);
            // iccid
            start += 8;
            String iccid = HexUtils.strChangeStrByLengthDesc(datas, start, 10, true);
            iccid = HexUtils.getHexNumByFixed(iccid.split(" "), "33", false);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("meterNo", meterNo);
            jsonObject.put("result", Double.parseDouble(result) * 0.01);
            jsonObject.put("result1", Double.parseDouble(result1) * 0.01);
            jsonObject.put("result2", Double.parseDouble(result2) * 0.01);
            jsonObject.put("result3", Double.parseDouble(result3) * 0.01);
            jsonObject.put("result4", Double.parseDouble(result4) * 0.01);
            jsonObject.put("csq", csq);
            jsonObject.put("order", content);
            return jsonObject;
        }catch (Exception e){
            log.error("数据解析错误：" + content + "数据解析错误：" + e.toString());
            return null;
        }
    }
}
